<?php

namespace Biz\service\esSearch;

use Biz\cache\brand\BrandScoreCache;
use Biz\cache\wiki\GoodsHot;
use Doraemon\service\esSearch\Common;

class WikiBasic
{
    public static function syncSave($data)
    {
        if (isset($data["data"]["id"]) && isset($data["data"]["claim_content_status"]) && isset($data["type"])) {
            if ($data["type"] == Common::TYPE_ADD) {
                self::saveData($data["data"]);
            } else if ($data["type"] == Common::TYPE_UPDATE) {
                self::saveData($data["data"]);
            }
        }
    }

    public static function saveData($wikiBasicInfo)
    {
        $brandInfo = \Biz\service\brand\Brand::getBrandInfoById($wikiBasicInfo["brand_id"]);
        $wikiParam = array(
            "basic_id" => $wikiBasicInfo["id"],
            "is_default" => 1
        );
        $wikiInfo = \Biz\model\wiki\Wiki::getByWhere($wikiParam);
        $rdsGoodsHot = GoodsHot::getGoodsHotById($wikiBasicInfo["id"]);
        $goodsHot = $rdsGoodsHot ? $rdsGoodsHot : 0;

        $wikiBasicInfo["addtime"] = $wikiBasicInfo["addtime"] !== "0000-00-00 00:00:00" ? $wikiBasicInfo["addtime"] : 0;
        $wikiBasicInfo["edittime"] = $wikiBasicInfo["edittime"] !== "0000-00-00 00:00:00" ? $wikiBasicInfo["edittime"] : 0;

        $brandAlias = $brandInfo["brand_alias"] ? json_decode($brandInfo["brand_alias"]) : "";
        $formatData = array(
            "wiki_id" => $wikiInfo["id"] ?? 0,
            "brand_uid" => $brandInfo["uid"],
            "brand_id" => $wikiBasicInfo["brand_id"],
            "title" => $wikiBasicInfo["title"],
            "wiki_title" => $wikiInfo["title"],
            "goods_hot" => $goodsHot,
            "min_price" => $wikiInfo["min_price"] ?? 0,
            "max_price" => $wikiInfo["max_price"] ?? 0,
            "cover_img" => $wikiInfo["cover_img"] ?? "",
            "series" => trim(str_replace(",", " ", $wikiBasicInfo["series"])),
            "style" => trim(str_replace(",", " ", $wikiBasicInfo["style"])),
            "material" => trim(str_replace(",", " ", $wikiBasicInfo["material"])),
            "space" => trim(str_replace(",", " ", $wikiBasicInfo["space"])),
            "brand_name" => isset($brandInfo["brand_name"]) && $brandInfo["brand_name"] ? $brandInfo["brand_name"] : "",
            "brand_alias" => $brandAlias ? implode(" ", $brandAlias) : "",
            "brand_type" => $brandInfo["brand_type"],
            "category_id" => $wikiBasicInfo["category_id"],
            "second_category_id" => $wikiBasicInfo["second_category_id"],
            "third_category_id" => $wikiBasicInfo["third_category_id"],
            "four_category_id" => $wikiBasicInfo["four_category_id"],
            "status" => $wikiBasicInfo["basic_state"],
            "wiki_status" => $wikiInfo["status"] ?? 0,
            "channel_spu_status" => $wikiInfo["channel_spu_status"] ?? 1,
            "addtime" => strtotime($wikiBasicInfo["addtime"]),
            "edittime" => strtotime($wikiBasicInfo["edittime"]),
        );

        // 品牌指数
        $brandScore = 0;
        if (intval($wikiBasicInfo["brand_id"]) > 0) {
            $brandScore = BrandScoreCache::getBrandScore($wikiBasicInfo["brand_id"]);
            $brandScore = empty($brandScore) ? 0 : $brandScore;
        }
        $formatData["brand_score"] = $brandScore;

        // 品牌权益
        $formatData['brand_pack_type'] = 0;
        $formatData['brand_pack_type'] = $brandInfo["pack_type"];

        // 品牌权益
        $formatData['brand_rights'] = "";
        $brandRights = \Biz\service\BrandRights\BrandRightsAllService::getBrandSearchSource($wikiBasicInfo["brand_id"]);

        if (is_array($brandRights) && isset($brandRights[$wikiBasicInfo["brand_id"]]) && $brandRights[$wikiBasicInfo["brand_id"]]) {
            $formatData["brand_rights"] = implode(" ", $brandRights[$wikiBasicInfo["brand_id"]]);
        }

        \Doraemon\service\es\EsWikiBasic::post($wikiBasicInfo["id"], $formatData);
    }

    public static function saveDataById($id)
    {
        $wikiBasicInfo = \Biz\model\wiki\WikiBasic::getByWhere(array(
            "id" => $id
        ));

        self::saveData($wikiBasicInfo);
    }
}